package com.hdl;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson.JSONObject;
import com.hdl.config.ConfigurationCenter;
import com.hdl.config.DataSourceProperties;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.BoundedExponentialBackoffRetry;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.data.Stat;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

/**
 * @author Administrator
 */
@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
@ComponentScan(basePackages = {"com.hdl"})
public class ServerBootstrap {
    public static void main(String[] args) {
        final ConfigurableApplicationContext run = SpringApplication.run(ServerBootstrap.class);
        final Object bean = run.getBean(ConfigurationCenter.CONFIG_CENTER_DATASOURCE);
        System.out.println(bean);
    }
//        CuratorFramework zkClient = CuratorFrameworkFactory.builder()
//                .retryPolicy(new ExponentialBackoffRetry(1000, 3))
//                .namespace("hdlrpc")
//                .connectString("192.168.227.131:2181")
//                .connectionTimeoutMs(5000)
//                .sessionTimeoutMs(5000)
//                .build();
//        zkClient.start();
//        try {
//            Stat stat = zkClient.checkExists().forPath(ConfigurationCenter.DATABASE_CONFIGURATION);
//            DataSourceProperties properties = setProperties();
//            if (stat == null) {
//                zkClient.create().forPath(ConfigurationCenter.DATABASE_CONFIGURATION, JSONObject.toJSONBytes(properties));
//            }
//
//            final byte[] bytes = zkClient.getData().usingWatcher(new Watcher() {
//                @Override
//                public void process(WatchedEvent watchedEvent) {
//                    if (watchedEvent.getType() == Event.EventType.NodeDataChanged) {
//                        System.out.println("节点内容变更通知...");
//                        main(args);
//                    }
//                }
//            }).forPath(ConfigurationCenter.DATABASE_CONFIGURATION);
//
//            DataSourceProperties o = JSONObject.parseObject(bytes, DataSourceProperties.class);
//            System.out.println(o);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        try {
//            Thread.sleep(Integer.MAX_VALUE);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//    }
//
//    private static DataSourceProperties setProperties(){
//        DataSourceProperties properties = new DataSourceProperties();
//        properties.setDriverClassName("com.mysql.jdbc.Driver");
//        properties.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/jpa");
//        properties.setUsername("root");
//        properties.setPassword("root");
//        properties.setMaxActive(30);
//        properties.setMaxIdle(10);
//        return properties;
//    }
//
//    private static DruidDataSource setDataSourceByProperties(DataSourceProperties properties){
//        DruidDataSource druidDataSource = new DruidDataSource();
//        druidDataSource.setDriverClassName(properties.getDriverClassName());
//        druidDataSource.setUrl(properties.getJdbcUrl());
//        druidDataSource.setUsername(properties.getUsername());
//        druidDataSource.setPassword(properties.getPassword());
//        druidDataSource.setMaxActive(properties.getMaxActive());
//        druidDataSource.setMaxIdle(properties.getMaxIdle());
//        return druidDataSource;
//    }
}